package com.utils.catche;

import java.util.Set;

import javax.annotation.Resource;

import org.springframework.beans.factory.annotation.Value;



public class CacheUtils {
	private static CacheManager cacheManager;

    private static String defaultCache = "local";

    private static int cacheIndex = 0;

    @Resource(name = "wdlsCacheManager")
    public void setCacheManager(CacheManager cacheManager) {
        CacheUtils.cacheManager = cacheManager;
    }

    @Value("${module.cache.util.default:local}")
    public void setDefaultCache(String defaultCache) {
        CacheUtils.defaultCache = defaultCache;
    }

    private static CacheSupport getCacheSupport(Integer type) {
        if (cacheManager == null) {
            throw new RuntimeException("缓存未初始化");
        }
        String key = defaultCache;
        if(type.equals(CacheType.LOCAL)){
            key = "local";
        }else if(type.equals(CacheType.REMOTE)){
            key = "remote";
        }else if(type.equals(CacheType.SERVLET)){
            key = "servlet";
        }
        return (CacheSupport) cacheManager.getCache(key);
    }

    /**
     * 存入缓存对象
     *
     * @param type  缓存类型
     * @param key
     * @param value
     */
    public static void put(Integer type, String key, Object value) {
        CacheSupport cacheSupport = getCacheSupport(type);
        cacheSupport.put(key, value);
    }

    /**
     * 存入缓存对象
     *
     * @param key
     * @param value
     */
    public static void put(String key, Object value) {
        put(CacheType.DEFAULT, key, value);
    }

    /**
     * 存入缓存对象，带过期时间（闲置时间）
     *
     * @param type       缓存类型
     * @param key
     * @param value
     * @param timeToIdle
     */
    public static void put(Integer type, String key, Object value, int timeToIdle) {
        CacheSupport cacheSupport = getCacheSupport(type);
        cacheSupport.put(key, value, timeToIdle);
    }


    /**
     * 存入缓存对象，带过期时间（闲置时间）
     *
     * @param key
     * @param value
     * @param timeToIdle
     */
    public static void put(String key, Object value, int timeToIdle) {
        put(CacheType.DEFAULT, key, value, timeToIdle);
    }

    /**
     * 获取缓存对象
     *
     * @param type 缓存类型
     * @param key
     * @return
     */
    public static Object get(Integer type, String key) {
        CacheSupport cacheSupport = getCacheSupport(type);
        return cacheSupport.get(key);
    }

    /**
     * 获取缓存对象
     *
     * @param key
     * @return
     */
    public static Object get(String key) {
        return get(CacheType.DEFAULT, key);
    }

    /**
     * 获取缓存对象并更新过期时间
     *
     * @param type       缓存类型
     * @param key
     * @param timeToIdle
     * @return
     */
    public static Object get(Integer type, String key, int timeToIdle) {
        CacheSupport cacheSupport = getCacheSupport(type);
        return cacheSupport.get(key, timeToIdle);
    }


    /**
     * 获取缓存对象并更新过期时间
     *
     * @param key
     * @param timeToIdle
     * @return
     */
    public static Object get(String key, int timeToIdle) {
        return get(CacheType.DEFAULT, key, timeToIdle);
    }


    /**
     * 移除缓存
     *
     * @param type 缓存类型
     * @param key
     */
    public static void remove(Integer type, String... keys) {
        CacheSupport cacheSupport = getCacheSupport(type);
        cacheSupport.remove(keys);
    }


    /**
     * 移除缓存
     *
     * @param key
     */
    public static void remove(String... keys) {
        remove(CacheType.DEFAULT, keys);
    }

    /**
     * 判断缓存是否存在
     *
     * @param type 缓存类型
     * @param key
     * @return
     */
    public static boolean hasKey(Integer type, String key) {
        CacheSupport cacheSupport = getCacheSupport(type);
        return cacheSupport.hasKey(key);
    }

    /**
     * 判断缓存是否存在
     *
     * @param key
     * @return
     */
    public static boolean hasKey(String key) {
        return hasKey(CacheType.DEFAULT, key);
    }

    /**
     * 在Set中添加数据
     *
     * @param type  缓存类型
     * @param key
     * @param value
     */
    public static void putSet(Integer type, String key, String... values) {
        CacheSupport cacheSupport = getCacheSupport(type);
        cacheSupport.putSet(key, values);
    }

    /**
     * 在Set中添加数据
     *
     * @param key
     * @param value
     */
    public static void putSet(String key, String... values) {
        putSet(CacheType.DEFAULT, key, values);
    }

    /**
     * 判断value在Set中存在
     *
     * @param type  缓存类型
     * @param key
     * @param value
     * @return
     */
    public static boolean isKeyInSet(Integer type, String key, String... values) {
        CacheSupport cacheSupport = getCacheSupport(type);
        return cacheSupport.isKeyInSet(key, values);
    }

    /**
     * 判断value在Set中存在
     *
     * @param key
     * @param value
     * @return
     */
    public static boolean isKeyInSet(String key, String... values) {
        return isKeyInSet(CacheType.DEFAULT, key, values);
    }

    /**
     * 获取Set数据
     *
     * @param type 缓存类型
     * @param key
     * @return
     */
    public static Set<String> getSet(Integer type, String key) {
        CacheSupport cacheSupport = getCacheSupport(type);
        return cacheSupport.getSet(key);
    }

    /**
     * 获取Set数据
     *
     * @param key
     * @return
     */
    public static Set<String> getSet(String key) {
        return getSet(CacheType.DEFAULT, key);
    }


    /**
     * 从Set中删除值
     *
     * @param type  缓存类型
     * @param key
     * @param value
     */
    public static void removeSetValue(Integer type, String key, String... values) {
        CacheSupport cacheSupport = getCacheSupport(type);
        cacheSupport.removeSetValue(key, values);
    }

    /**
     * 从Set中删除值
     *
     * @param key
     * @param value
     */
    public static void removeSetValue(String key, String... values) {
        removeSetValue(CacheType.DEFAULT, key, values);
    }

    /**
     * 在Map中添加数据(字符串)
     *
     * @param type  缓存类型
     * @param key
     * @param field
     * @param value
     */
    public static void putMap(Integer type, String key, String field, String value) {
        CacheSupport cacheSupport = getCacheSupport(type);
        cacheSupport.putMap(key, field, value);
    }

    /**
     * 在Map中添加数据(字符串)
     *
     * @param key
     * @param field
     * @param value
     */
    public static void putMap(String key, String field, String value) {
        putMap(CacheType.DEFAULT, key, field, value);
    }

    /**
     * 获取map中的值
     *
     * @param type  缓存类型
     * @param key
     * @param field
     * @return
     */
    public static String getMapValue(Integer type, String key, String field) {
        CacheSupport cacheSupport = getCacheSupport(type);
        return cacheSupport.getMapValue(key, field);
    }

    /**
     * 获取map中的值
     *
     * @param key
     * @param field
     * @return
     */
    public static String getMapValue(String key, String field) {
        return getMapValue(CacheType.DEFAULT, key, field);
    }

    /**
     * 从Map中删除
     *
     * @param type  缓存类型
     * @param key
     * @param field
     */
    public static void removeMapField(Integer type, String key, String field) {
        CacheSupport cacheSupport = getCacheSupport(type);
        cacheSupport.removeMapField(key, field);
    }

    /**
     * 从Map中删除
     *
     * @param key
     * @param field
     */
    public static void removeMapField(String key, String field) {
        removeMapField(CacheType.DEFAULT, key, field);
    }
}
